package com.jcp.controller;


import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.SessionAttributes;

import com.jcp.entity.Users;
import com.jcp.service.PayService;
import com.jcp.service.UserService;

@Controller
@RequestMapping(method=RequestMethod.GET)
@SessionAttributes("user")
public class UserController {
	//UserService的依赖注入
	@Autowired
	private  UserService userservice;
	
	@Autowired
	private PayService payservice;
	
	/**
	 * 登录的方法
	 * @param user：Users对象
	 * @return String
	 */
	@PostMapping(value="login",produces="application/json;charset=utf-8")
	@ResponseBody
	public String LoginUser(HttpServletRequest hsr,@ModelAttribute(value="user")Users user) {
		
		//获取密码和手机号码
		String password=user.getPassword();
		String PhoneNumber=user.getPhoneNumber();
		System.out.println("password : "+password);
		System.out.println("PhoneNumber : "+PhoneNumber);
		if(password.equals("10086")&&PhoneNumber.equals("admin")) {
			return "管理员登录";
		}
		//获取User模糊查询出来的集合
		Users userlist = userservice.selectByOne(user);
		System.out.println("userlist : "+userlist);
		if(userlist!=null) {//用户登录
			hsr.getSession().setAttribute("mac","电脑");
			//给session中设置用户名
			user.setUserName(userlist.getUserName());
			//给session中设置用户名
			user.setUserName(userlist.getUserName());
			user.setCarNumber(userlist.getCarNumber());
			user.setMembership_expires_date(userlist.getMembership_expires_date());
			user.setRemainder(userlist.getRemainder());
			System.out.println(user);
			return "登录成功";
		}else {
		return "登录失败";//登录失败
		}
	}
	//解决sessio的异常
	@ModelAttribute("user")
	public Users getUser() {
		Users user = new Users();
		return user;
	}

	@GetMapping(value="tiaoalibaba222")
	public String tiaoalibaba(@ModelAttribute(value="user")Users user,HttpServletRequest request) throws Exception {
		//获取User模糊查询出来的集合
		Users userlist = userservice.LoginUser(user);
			//给session中设置用户名
		double remainder = Double.parseDouble(request.getSession().getAttribute("addadd").toString());
		userservice.addparmt1(request.getSession().getAttribute("addadd").toString(), userlist.getCarNumber());
		//当充值为冲会员的时候
		if(remainder==700.0 || userlist.getMembership_expires_date()==null) {
			
			if(userlist.getMembership_expires_date()==null) {
				//新人
				Date date = new Date();
				SimpleDateFormat smdf = new SimpleDateFormat("yyyy-MM-dd");
				Calendar cal = Calendar.getInstance();
				cal.setTime(date);   //设置当前时间
				cal.add(Calendar.MONTH,1);
				String huiyuanshijian = smdf.format(cal.getTime());
				payservice.JCPhuiyuan(huiyuanshijian, userlist.getPhoneNumber());
				user.setMembership_expires_date(huiyuanshijian);
				remainder=0;
				
				
				
			}else {
				//冲过会员
				Date date = new Date();
				SimpleDateFormat smdf = new SimpleDateFormat("yyyy-MM-dd");
				//获取数据库的 会员到期时间
				long shuju = smdf.parse(userlist.getMembership_expires_date()).getTime();
				//当前 时间  毫秒值
				long xx =date.getTime();
				//如果会员已过期
				if((shuju-xx)<=0 ) {
					Calendar cal = Calendar.getInstance();
					cal.setTime(date);   //设置当前时间
					cal.add(Calendar.MONTH,1);
					String huiyuanshijian = smdf.format(cal.getTime());
					payservice.JCPhuiyuan(huiyuanshijian, userlist.getPhoneNumber());
					user.setMembership_expires_date(huiyuanshijian);
					remainder=0;
				}else {
					Calendar cal1 = Calendar.getInstance();
					Date daate1= new Date(shuju);
					cal1.setTime(daate1);
					cal1.add(Calendar.MONTH, 1);
					String huiyuanshijian = smdf.format(cal1.getTime());
					payservice.JCPhuiyuan(huiyuanshijian, userlist.getPhoneNumber());
					user.setMembership_expires_date(huiyuanshijian);
					remainder=0;
				}
			}
		}
		user.setUserName(userlist.getUserName());
		user.setCarNumber(userlist.getCarNumber());
		user.setRemainder(userlist.getRemainder()+remainder);
//		payservice.addremainder(remainder, phoneNumber)
		payservice.addremainder(remainder, user.getPhoneNumber());
		return "forward:PaySystem.jsp";
	}
	
	
	/**
	 * 登录界面的注册方法
	 * @param users：User对象
	 * @return
	 */
	@PostMapping(value="ZhuChe",produces="application/json;charset=utf-8")
	@ResponseBody
	public String ZhuCheUser(@ModelAttribute(value = "users")Users users) {
		//判断用户是否存在
		if(userservice.selectChong(users)||(users.getPassword().length()==0)) {
			return "注册失败";
		}else {
			if(userservice.ZhuCheUser(users)) {
				return "注册成功";
			}else {
				return "注册失败";
			}
			
		}
	}
	
	
	
	
	
}
